<template>

  <!--
    作者：luoyiming
    时间：2020-06-04
    描述：插件中心-积分商城-商品列表
  -->
  <div class="user" v-loading="loading">
    <el-tabs v-model="activeName" @tab-change="handleClick">
      <el-tab-pane label="兌換中" name="hasAudit"></el-tab-pane>
      <el-tab-pane :label="'待審核(' + applyCount + ')'" name="waitAudit"></el-tab-pane>
    </el-tabs>
    <div class="common-level-rail">
      <el-button size="small" type="primary" icon="Plus" @click="openProduct()">商品報名</el-button>
    </div>
    <div class="product-content point-list">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%">
          <el-table-column prop="productName" label="商品名稱" width="360">
            <template #default="scope">
              <div class="product-info">
                <div class="pic" style="width: 40px; height: 40px;"><img v-img-url="scope.row.imagePath"
                    alt="" /></div>
                <div class="info">
                  <div class="name">{{ scope.row.productName }}</div>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="規格">
            <template #default="scope">
              <span v-if="scope.row.specType == 10">單規格</span>
              <span v-if="scope.row.specType == 20">多規格</span>
            </template>
          </el-table-column>
          <el-table-column label="積分">
            <template #default="scope">
              {{ scope.row.sku[0].pointNum }}
            </template>
          </el-table-column>
          <el-table-column label="金額">
            <template #default="scope">
              <span class="orange fb">{{ scope.row.sku[0].pointMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="limitNum" label="限購數量"></el-table-column>
          <el-table-column prop="stock" label="活動庫存"></el-table-column>
          <el-table-column prop="sort" label="排序"></el-table-column>
          <el-table-column label="狀態">
            <template #default="scope">
              <span v-if="scope.row.status == 0" class="green">待審核</span>
              <span v-if="scope.row.status == 10" class="green">通過</span>
              <span v-if="scope.row.status == 20" class="gray">未通過</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="備註"></el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <el-button @click="edit(scope.row)" type="text" size="small">修改</el-button>
              <el-button @click="delClick(scope.row)" type="text" size="small">刪除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination background :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"></el-pagination>
      </div>
    </div>

    <!--商品选择-->
    <Product :isproduct="isproduct" :excludeIds="excludeIds" :islist="false" @closeDialog="closeProductFunc"></Product>
  </div>
</template>

<script>
import PointsApi from '@/api/points';
import Product from '@/components/product/Product.vue';
export default {
  components: {
    /*选择商品*/
    Product
  },
  data() {
    return {
      formInline: {},
      form: {},
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否加载完成*/
      loading: true,
      /*是否打开选择商品*/
      isproduct: false,
      /*已有的id*/
      excludeIds: [],
      /*已有的id*/
      activeName: 'hasAudit',
      // 待审核数量
      applyCount: 0
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*获取列表*/
    getData() {
      let self = this;
      let Params = {};
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      Params.status = self.activeName;
      PointsApi.myList(Params, true).then(res => {
        self.loading = false;
        self.tableData = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.excludeIds = res.data.excludeIds;
        self.applyCount = res.data.applyCount;
      }).catch(error => {});
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getData();
    },
    /*打开添加*/
    openProduct() {
      this.isproduct = true;
    },
    /*切换菜单*/
    handleClick(tab) {
      let self = this;
      self.curPage = 1;
      self.activeName = tab;
      self.getData();
    },
    /*关闭商品选择*/
    closeProductFunc(e) {
      this.isproduct = false;
      if (e.type == 'success') {
        this.$router.push('/activity/point/add?product_id=' + e.params.productId);
      }
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    edit(row) {
      let self = this;
      ElMessageBox.confirm("修改後會自動下架該商品，並且需要平台重新審核，確認修改該紀錄嗎？", "提示", {
        type: 'warning'
      }).then(() => {
        this.$router.push('/activity/point/edit?point_product_id=' + row.pointProductId);
      });
    },
    /*删除*/
    delClick: function (row) {
      let self = this;
      ElMessageBox.confirm("刪除後不可恢復，確認刪除該紀錄嗎？", "提示", {
        type: 'warning'
      }).then(() => {
        PointsApi.delProduct({
          pointProductId: row.pointProductId
        }).then(data => {
          ElMessage({
            message: "刪除成功",
            type: 'success'
          });
          self.getData();
        });
      });
    }
  }
};
</script>

